//
//  SwiftPageTitleView.swift
//  DouYuZB
//
//  Created by 中创 on 2020/4/20.
//  Copyright © 2020 梁森. All rights reserved.
//

import UIKit

protocol SwiftPageTitleViewDelegate : class {
    func pageTitleView(page:SwiftPageTitleView, selectedIndex index:Int)
}

// MARK:- 定义常量
private let kScrollLineH : CGFloat = 2
private let kNormalColor : (CGFloat, CGFloat, CGFloat) = (85, 85, 85)
private let kSelectedColor : (CGFloat, CGFloat, CGFloat) = (255, 128, 0)

class SwiftPageTitleView: UIView {

    weak var deleate:SwiftPageTitleViewDelegate?
    private let titles:[String]
    private var titleLbls:[UILabel] = [UILabel]()
    private var currentIndex:Int = 0
    init(frame: CGRect, titles:[String]) {
        self.titles = titles
        super.init(frame: frame)
        backgroundColor = UIColor.white
        setupSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:懒加载
    private lazy var scrollView:UIScrollView = {
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
        scrollView.scrollsToTop = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()
    private lazy var scrollLine:UIView = {
        let scrollLine:UIView = UIView()
        scrollLine.backgroundColor = UIColor.orange
        return scrollLine
    }()
    
}
// MARK:设置UI相关
extension SwiftPageTitleView{
    private func setupSubviews() {
        addSubview(scrollView)
        
        let lblWidth = self.bounds.size.width / CGFloat(self.titles.count)
        let lblHeight = self.bounds.size.height - kScrollLineH
        for (index, title) in self.titles.enumerated() {
            let titleLbl:UILabel = UILabel(frame: CGRect(x: lblWidth * CGFloat(index), y: 0, width: lblWidth, height: lblHeight))
            titleLbl.text = title
            titleLbl.font = UIFont.systemFont(ofSize: 16)
            titleLbl.textAlignment = .center
            titleLbl.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
            titleLbl.tag = index
            scrollView.addSubview(titleLbl)
            titleLbls.append(titleLbl)
            let tap = UITapGestureRecognizer(target: self, action: #selector(clickTitleLbl(tap:)))
            titleLbl.isUserInteractionEnabled = true
            titleLbl.addGestureRecognizer(tap)
        }
        
        let bottomLine:UIView = UIView(frame: CGRect(x: 0, y: bounds.size.height-0.5, width: self.bounds.size.width, height: 0.5))
        bottomLine.backgroundColor = UIColor.lightGray
        addSubview(bottomLine)
        
        guard let firstLbl:UILabel = titleLbls.first else {return}
        firstLbl.textColor = UIColor(r: kSelectedColor.0, g: kSelectedColor.1, b: kSelectedColor.2)
        scrollLine.frame = CGRect(x: firstLbl.frame.origin.x, y: lblHeight, width: lblWidth, height: kScrollLineH)
        addSubview(scrollLine)
    }
}
// MARK:action
extension SwiftPageTitleView{
    @objc func clickTitleLbl(tap:UITapGestureRecognizer) {
        let currentLbl = tap.view as! UILabel
        if currentLbl.tag == currentIndex {return}
        currentLbl.textColor = UIColor(r: kSelectedColor.0, g: kSelectedColor.1, b: kSelectedColor.2)
        let oldLbl = titleLbls[currentIndex]
        oldLbl.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
        currentIndex = currentLbl.tag

        UIView.animate(withDuration: 0.25) {
            self.scrollLine.frame.origin.x = currentLbl.frame.origin.x
        }

        deleate?.pageTitleView(page: self, selectedIndex: currentLbl.tag)
    }
}
// MARK:对外暴露的方法
extension SwiftPageTitleView{
    func setScrollIndex(progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
//        print("\(progress), \(sourceIndex), \(targetIndex)")
        let sourceLbl = titleLbls[sourceIndex]
        let targetLbl = titleLbls[targetIndex]
        let totalMoveX = targetLbl.frame.origin.x - sourceLbl.frame.origin.x
        let moveLength = totalMoveX * progress
        scrollLine.frame.origin.x = sourceLbl.frame.origin.x + moveLength
        let colorDelta = (kSelectedColor.0 - kNormalColor.0, kSelectedColor.1 - kNormalColor.1, kSelectedColor.2 - kNormalColor.2)
       
        // 最后的sourceLbl和targetLbl是同一个
        sourceLbl.textColor = UIColor(r: kSelectedColor.0-colorDelta.0*progress, g: kSelectedColor.1-colorDelta.1*progress, b: kSelectedColor.2-colorDelta.2*progress)
        targetLbl.textColor = UIColor(r: kNormalColor.0+colorDelta.0*progress, g: kNormalColor.1+colorDelta.1*progress, b: kNormalColor.2+colorDelta.2*progress)
        currentIndex = targetIndex
    }

}
